/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'zmnApp', 'zmnSelect'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;

    // 师傅下拉框实例保存
    let masterSelect;

    form.on('radio(engineerRole)', function (data) {
        if (data.value == 1) {
            $("#accountTypeLabel1").show();
            $("#accountTypeLabel2").hide();
        } else if (data.value == 2) {
            $("#accountTypeLabel2").show();
            $("#accountTypeLabel1").hide();
        } else {
            console.log("Error~ 单选项异常")
        }
    });

    layer.ready(function () {
        if (json.vo.engineerRole == 1) {
            $("#accountTypeLabel1").show();
            $("#accountTypeLabel2").hide();
        } else if (json.vo.engineerRole == 2) {
            $("#accountTypeLabel2").show();
            $("#accountTypeLabel1").hide();
        } else {
            $("#accountTypeLabel1").show();
        }
        form.render();
    });


    zmnSelect.selectPlatSubCompanySpCompany({
        platSelectId: 'plat',
        subCompanySelectId: 'subCompanyId',
        spCompanySelectId: 'spCompanyId',
    }, {
        platSelectId: {
            selectedValues: json.vo.plat,
            isRadio: true,
            remote: {
                limitPermit: true,
            },
            events: {
                changed: function (values) {
                    console.log("==========platId4=========");
                    console.log("已选中的值:" + values);
                    json.vo.plat=values[0];
                }
            }
        },
        subCompanySelectId: {
            selectedValues: json.vo.subCompanyId,
            isRadio: true,
            events: {
                changed: function (values) {
                    console.log("==========subCompanyId4=========");
                    console.log("已选中的值:" + values);
                }
            }
        },
        spCompanySelectId: {
            selectedValues: json.vo.spCompanyId,
            isRadio: true,
            remote: {
                limitPermit: true,
            },
            events: {
                changed: function (values) {
                    console.log("==========spCompanySelectId=========");
                    console.log("已选中的值:" + values);
                }
            }
        }
    });


    zmnSelect.searchEngineer('engineerId', {
        searchedValue: json.vo.realName,
        selectedValues: json.vo.engineerId,
        isRadio: true,
        remote: {
            limitPermit: false,
            events: {
                extendParams: function () {
                    return {
                        plat: json.vo.plat==null?10:json.vo.plat,
                    }
                }
            }
        },
        events: {
            changed: function (values) {
                console.log("========engineerIds=========")
                console.log("已选中的值:" + values)
            }
        }
    });
    $('#engineerId .xm-search-input').on('input propertychange', function () {
        json.vo.realName = $('#engineerId .xm-search-input').val();
    });

    zmnSelect.searchEngineerSupervisor('engineerSupervisorIds', {
        searchedValue: json.vo.realName,
        selectedValues: json.vo.engineerId,
        isRadio: true,
        remote: {
            limitPermit: false,
            events: {
                extendParams: function () {
                    return {
                        plat: json.vo.plat==null?10:json.vo.plat,
                    }
                }
            }
        },
        events: {
            changed: function (values) {
                console.log("========engineerSupervisorIds=========")
                console.log("已选中的值:" + values)
            }
        }
    });
    $('#engineerSupervisorIds .xm-search-input').on('input propertychange', function () {
        json.vo.realName = $('#engineerSupervisorIds .xm-search-input').val();
    });


    // 点击保存
    form.on('submit(zmn-engineer-binding-form-submit)', function (obj) {
        var engineerId="";
        if (obj.field.plat == null || obj.field.plat == "") {
            layer.msg('平台不能为空');
            return;
        }
        if (obj.field.subCompanyId == null || obj.field.subCompanyId == "") {
            layer.msg('子公司不能为空');
            return;
        }
        if (obj.field.spCompanyId == null || obj.field.spCompanyId == "") {
            layer.msg('服务商不能为空');
            return;
        }
        if (obj.field.engineerRole == 1 && (obj.field.engineerId == null || obj.field.engineerId == "")) {
            layer.msg('工程师不能为空');
            return;
        }
        if (obj.field.engineerRole == 2 && (obj.field.engineerSupervisorIds == null || obj.field.engineerSupervisorIds == "")) {
            layer.msg('主管不能为空');
            return;
        }
        debugger
        zmnLayUtil.ajaxSimpleUE("/staff/bind/bindingStaffAndMaster.action", {
            staffId: json.vo.staffId,
            engineerId: obj.field.engineerId==""?obj.field.engineerSupervisorIds:obj.field.engineerId,
            engineerRole:obj.field.engineerRole,
        }, 0, function (json) {
            if (json.status == 200) {
                layer.msg("添加成功!", {time: 1000}, function () {
                    zmnLayUtil.closeThisIframe(parent);
                });
            } else {
                layer.msg(json.message, {"icon": 2});
            }
        });
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        zmnLayUtil.closeThisIframe(parent);
    });

    // layer.ready(function(){
    //     if (!zmnApp.isNull(json.plat)) {
    //         // 加载所属公司
    //         zmnApp.jsonAjax("/engineer/listCompanyByPlat.action?plat=" + json.plat, null, function (result) {
    //             if (result.status === 200) {
    //                 tempCompanyHtml = `<option value="">请选择所属公司</option>`;
    //                 result.data.forEach(items => {
    //                     let exAttr = "";
    //                     let exText = "";
    //                     let checked = items.value == json.companyId ? "selected=true" : "";
    //                     tempCompanyHtml += '<option value="' + items.value + '" ' + exAttr + checked + '>' + items.label + " " + exText + '</option>';
    //                 });
    //                 $("#companyId").html(tempCompanyHtml);
    //             } else {
    //                 zmnApp.msgFail(result.message);
    //             }
    //         });
    //     }
    //     if (!zmnApp.isNull(json.companyId)) {
    //         // 加载服务公司
    //         zmnApp.jsonAjax("/engineer/listCompanyBySonCompanyId.action?companyId=" + json.companyId + "&companyType=", null, function (result) {
    //             if (result.status === 200) {
    //                 tempCompanyHtml = `<option value="">请选择服务公司</option>`;
    //                 result.data.forEach(items => {
    //                     let exAttr = "";
    //                     let exText = "";
    //                     let checked = items.value == json.manageCompanyId ? "selected=true" : "";
    //                     tempCompanyHtml += '<option value="' + items.value + '" ' + exAttr + checked + '>' + items.label + " " + exText + '</option>';
    //                 });
    //                 $("#manageCompanyId").html(tempCompanyHtml);
    //             } else {
    //                 zmnApp.msgFail(result.message);
    //             }
    //         });
    //     }
    // });

    // /**
    //  * 清空工程师下拉框的选项
    //  * @param selectItem
    //  */
    // function clearSelectOptions(selectItem) {
    //     selectItem.update({
    //         data: []
    //     })
    // }

    //对外暴露的接口
    exports('staff_bind_master', {});
});